﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AAPCVM.Interfaces;
using System.IO;
using AAPEVM;
using System.Drawing.Printing;
namespace AAPCVM
{
	public partial class frmVote : Form
	{
		IController _controller = new Controller();
		ctrlCandidateInfo[] list = new ctrlCandidateInfo[10];
		public Candidate[] CandidateList;
		int BallotId;
		ErrorProvider errProvider = new ErrorProvider();
		public frmVote()
		{
			InitializeComponent();
			_controller.OnNOTA += OnNOTA;
		}

		private void Vote_Load(object sender, EventArgs e)
		{
			FileManager.PrepareXMLFromCSV();

			Point start = new Point(20, 20);

			for (int i = 0; i < 10; i++)
			{
				ctrlCandidateInfo obj = CreateControl(start);
				list[i] = obj;
				this.Controls.Add(obj);
				start.Y = start.Y + obj.Height + 4;
				obj.InitializeControl(_controller);

				if (i == 4)
				{
					start.X = start.X + obj.Width + 20;
					start.Y = 20;
				}
			}
			btnVote.Location = new Point(18, start.Y + 10);

			list[0].Focus();

			Login frmLogin = new Login();

			if (frmLogin.ShowDialog() != System.Windows.Forms.DialogResult.OK)
			{
				MessageBox.Show("Invalid credentials. Application will now close. \n \nPlease try again with correct credentials.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				Application.Exit();
			}
			BallotId = frmLogin.BallotID;
			this.Text += " - Ballot Id : " + BallotId.ToString();
		}

		private ctrlCandidateInfo CreateControl(Point start)
		{
			ctrlCandidateInfo obj = new ctrlCandidateInfo();
			obj.Location = new Point(start.X, start.Y);
			return obj;
		}

		private void OnNOTA(string candidate)
		{
			foreach (var item in list)
			{
				item.Enabled = false;
			}
		}

		private void btnRePrint_Click(object sender, EventArgs e)
		{
			Login frmLogin = new Login();
			frmLogin.BallotID = BallotId;
			if (frmLogin.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				RePrint();
			}
		}

		private void RePrint()
		{
			PrintHandler.PrintSelection(CandidateList);
		}

		private void btnCancelVote_Click(object sender, EventArgs e)
		{
			Login frmLogin = new Login();
			frmLogin.BallotID = BallotId;
			if (frmLogin.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				CancelLastVote();
			}
		}

		private void CancelLastVote()
		{
			/*Create Candidate Id List*/
			string[] CandidateIdList = new string[CandidateList.Length];
			for (int i = 0; i < CandidateList.Length; i++)
			{
				CandidateIdList[i] = CandidateList[i].CandidateID;
			}

			/*Send list to File manager to merge it with master list.*/
			FileManager.DecreaseVotes(CandidateIdList);
		}

		private void btnVote_Click(object sender, EventArgs e)
		{

			
			if (!ValidateInputData())
			{
				list[0].Focus();
				return;
			}

			/*Create list of Selected Candidates.*/
			string[] lst = CreateSelectedCandidateList();

			/*Add Voted to Master List.*/
			AddVotes(lst);
			
			/*Print Document of Selected Candidates.*/
			PrintHandler.PrintSelection(CandidateList);
			ScreenReset();
		}

		private DataGridView CreateGridForCandidates(Candidate[] CandidateList)
		{
			DataGridView candidateGrid = new DataGridView();
			
			DataGridViewColumn dgvcID = new DataGridViewColumn();
			dgvcID.HeaderText = "ID";
			dgvcID.CellTemplate = new DataGridViewTextBoxCell();
			dgvcID.Width = 50;

			candidateGrid.Columns.Add(dgvcID);
			
			DataGridViewColumn dgvcName = new DataGridViewColumn();
			dgvcName.HeaderText = "Candidate Name";
			dgvcName.CellTemplate = new DataGridViewTextBoxCell();
			candidateGrid.Columns.Add(dgvcName);

			foreach (var candidate in CandidateList)
			{
				int row = candidateGrid.Rows.Add();
				DataGridViewRow newRow = candidateGrid.Rows[row];
				newRow.Cells[0].Value = candidate.CandidateID;
				newRow.Cells[1].Value = candidate.Name;

				
			}
			return candidateGrid;
		}

		private bool ValidateInputData()
		{

			if (list[0].CandidateId == "00")
				return true;

			foreach (var item in list)
			{
				if (item.CandidateId == string.Empty)
					return false;
			}

			return true;
		}

		private void AddVotes(string[] CandidateIdList)
		{
			/*Send list to File manager to merge it with master list.*/
			CandidateList = FileManager.IncreaseVotes(CandidateIdList);
		}

		private string[] CreateSelectedCandidateList()
		{
			List<string> candidateList = new List<string>();

			for (int i = 0; i < list.Length; i++)
			{
				if (list[i].CandidateId != string.Empty)
				{
					candidateList.Add(list[i].CandidateId);
				}	 
			}

			return candidateList.ToArray();
		}

		private void btnGenerateReport_Click(object sender, EventArgs e)
		{
			Login frmLogin = new Login();
			frmLogin.BallotID = BallotId;
			if (frmLogin.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				GenerateReport();
			}
		}

		private void GenerateReport()
		{
			/*Create CSV File .*/
			FileManager.PrepareResultsFile();
			string filepath = AppDomain.CurrentDomain.BaseDirectory + "Candidates" + "\\Results.csv";

			PrintHandler.PrintStreamReaderData(new StreamReader(filepath, Encoding.UTF8, false));
		}

		private void ScreenReset()
		{
			foreach (var item in list)
			{
				item.Reset();
				item.Enabled = true;
			}

			_controller.Reset();

			SetFocus(ControlEnum.FirstDigit);
		}

		private void SetFocus(ControlEnum controlEnum)
		{

			switch(controlEnum)
			{
				case ControlEnum.VoteButton:
					btnVote.Focus();
					break;

				default:
					list[0].Focus();
					break;
			}
		}
	}
}
